<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="confirmCode">
              <a-input v-model="model.confirmCode" disabled placeholder="请输入单据编号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="生产班组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="team">
              <j-search-select-tag v-model="model.team" 
              :dictOptions="teamDictOptions"
              placeholder="请选择生产班组" />
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row :gutter="16" style="margin-bottom: 10px;">
            <a-card title="生产过程确认">
              <a-row class="label-left">

                <a-col :span="24">
                  <a-form-model-item label="投料与配方卡一致" :labelCol="switchLabelCol" :wrapperCol="switchWrapperCol" prop="materialRecipeConsistent">
                    <j-search-select-tag v-model="model.materialRecipeConsistent" :dictOptions="yesNoDictOptions" placeholder="请选择" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="24">
                  <a-form-model-item label="预混的小料投放与配方卡一致" :labelCol="switchLabelCol" :wrapperCol="switchWrapperCol" prop="ingredientRecipeConsistent">
                    <j-search-select-tag v-model="model.ingredientRecipeConsistent" :dictOptions="yesNoDictOptions" placeholder="请选择" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="24">
                  <a-form-model-item label="中心线按时记录，符合要求，有异常点时进行了分析和记录" :labelCol="switchLabelCol" :wrapperCol="switchWrapperCol" prop="centerLineRecorded">
                    <j-search-select-tag v-model="model.centerLineRecorded" :dictOptions="yesNoDictOptions" placeholder="请选择" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="24">
                  <a-form-model-item label="开停车转产按照要求进行检查记录" :labelCol="switchLabelCol" :wrapperCol="switchWrapperCol" prop="startStopCarRecord">
                    <j-search-select-tag v-model="model.startStopCarRecord" :dictOptions="yesNoDictOptions" placeholder="请选择" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="24">
                  <a-form-model-item label="记录遵循正确原则" :labelCol="switchLabelCol" :wrapperCol="switchWrapperCol" prop="recordCorrectPrinciple">
                    <j-search-select-tag v-model="model.recordCorrectPrinciple" :dictOptions="yesNoDictOptions" placeholder="请选择" />
                  </a-form-model-item>
                </a-col>
               
              </a-row>
            </a-card>
        </a-row>

        <a-row>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction, putAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ErpProductionProcessConfirmationBeforeForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      //新增功能的传值
      confirmationData: {
        type: Object,
        default: () => ({}),
        required: false
      }
    },
    data () {
      return {
        model:{
          confirmCode:'',
          workshopCode: '',
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        switchLabelCol: {
          xs: { span: 24 },
          sm: { span: 12 },
        },
        switchWrapperCol: {
          xs: { span: 24 },
          sm: { span: 12 },
        },
        confirmLoading: false,
        validatorRules: {
           confirmCode: [
              { required: true, message: '记录单号不能为空!'},
           ],
           team: [
              { required: true, message: '生产班组不能为空!'},
           ],
        },
        url: {
          beforeAdd: "/productionconfirmationbefore/erpProductionProcessConfirmationBefore/add",
          afterAdd: "/productionconfirmationafter/erpProductionProcessConfirmationAfter/add",

          edit: "/productionconfirmationbefore/erpProductionProcessConfirmationBefore/edit",
          queryById: "/productionconfirmationbefore/erpProductionProcessConfirmationBefore/queryById"
        },
        // 是否字典下拉
        yesNoDictOptions:[],
        // 班组字典
        teamDictOptions:[],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      // 获取yes_no字典
      this.loadYesNoDict();
      // 获取班组
      this.loadTeamDict();
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
        this.getSequenceNum()
        this.model.workshopCode = this.confirmationData.workshopCode;
        this.model.planId = this.confirmationData.planId;
        this.model.planCode = this.confirmationData.planCode;
      },
      getSequenceNum() {
        let url = '/sys/fillRule/executeRuleByCode/production_confirm_code'
        putAction(url, {}).then((res) => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            this.model.confirmCode = res.result
          }
        })
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              if(this.confirmationData.routePathType === '0'){
                //前配新增
                httpurl+=this.url.beforeAdd;
              }else if(this.confirmationData.routePathType === '1'){
                //后配新增
                httpurl+=this.url.afterAdd;
              }
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.$emit('submit-success'); // 新增触发提交成功事件
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      // 获取yes_no字典
      loadYesNoDict() {
        getAction(`/sys/dict/getDictItems/yes_no`,{}).then(res=>{
          if(res.success){
            this.yesNoDictOptions = res.result.map(item => ({value: item.value, text: item.text}))
          } else {
            this.yesNoDictOptions = []
          }
        })
      },
      // 加载班组
      loadTeamDict(){
        getAction(`/sys/sysDepart/querySuborderDepart`,{ orgCode: this.confirmationData.workshopCode }).then(res=>{
          if(res.success){
            this.teamDictOptions = res.result.map(item => ({value: item.orgCode, text: item.departName}))
          } else {
            this.teamDictOptions = []
          }
        })
      },
    }
  }
</script>
<style scoped>
  /* Label文本靠左显示 */
  .label-left :deep(.ant-form-item-label) {
    text-align: left !important;
    justify-content: flex-start !important;
  }
</style>